<template>
  <div class="goodsinfo-container">

    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 商品轮播区域 -->
    <div class="mui-card">
      <swipe :lunbotulist="lunbotulist" :isfull='false'></swipe>
    </div>
    
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
            <div class="price">
                <p>
                    市场价:<del>${{ goodsinfo.market_price }}</del>&nbsp;&nbsp;&nbsp;
                    <span class="new-price">销售价:${{ goodsinfo.sell_price }}</span>
                </p>
                <p>购买数量:<numberbox @getcount="numChange" :max="goodsinfo.stock_quantity"></numberbox></p>
                <p>
                    <mt-button type="primary" size="small">立即购买</mt-button>
                    <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
                </p>
            </div>
        </div>
      </div>
    </div>

    <!-- 商品详情区 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
            <p>商品货号:{{ goodsinfo.goods_no }}</p>
            <p>库存情况:{{ goodsinfo.stock_quantity }}</p>
            <p>上架时间:{{ goodsinfo.add_time | dataFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
            <mt-button plain size="large" type="primary" @click="goDesc(id)">图文介绍</mt-button>
            <mt-button plain size="large" type="danger" @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swipe from "../subcomponents/swipe";
import numberbox from '../shop/number-box'
import { Toast } from 'mint-ui';
export default {
  components: {
    swipe,
    numberbox
  },
  data() {
    return {
      id: this.$route.params.id,
      lunbotulist: [],
      goodsinfo:[],
      ballFlag:false,
      numBoxChange:1
    };
  },
  created() {
    this.getLunbotu();
    this.getgoodsinfo();
  },
  methods: {
    // 获取轮播图
    getLunbotu() {
      this.$axios.get("api/getthumimages/" + this.id).then(res => {
        if (res.data.status === 0) {
          this.lunbotulist = res.data.message;
        } else {
          Toast("请求错误");
        }
      });
    },
    // 获取商品信息
    getgoodsinfo(){
        this.$axios.get('api/goods/getinfo/'+this.id).then(res=>{
            if(res.data.status == 0){
                this.goodsinfo=res.data.message[0]
            }
        })
    },
    // 图文介绍
    goDesc(id){
      this.$router.push({ name:'godesc', params:{ id }})
    },

    //商品详情
    goComment(id){
      this.$router.push({ name:'gocomment', params:{ id }})
    },

    // 加入购物车动画
    addShopCar(){
      this.ballFlag=!this.ballFlag
    },
    beforeEnter(el){
      el.style.transform = "translate(0,0)"
    },
    enter(el, done){
      el.offsetWidth;

      // 获取小球在页面的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect()

      // 获取徽标位置
      const badgePosition = document.getElementById('badge').getBoundingClientRect()

      const xDist = badgePosition.left - ballPosition.left
      const yDist = badgePosition.top - ballPosition.top

      el.style.transform=`translate(${xDist}px,${yDist}px)`;
      el.style.transition="all .5s cubic-bezier(.17,.67,.65,1.07)";
      done()
    },
    afterEnter(el){
      this.ballFlag=!this.ballFlag
    },
    numChange(count){
      this.numBoxChange = count
      console.log(this.numBoxChange);
      
    }
  }
};
</script>

<style scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
}
.new-price{
    color: red;
    font-size: 16px;
    font-weight: 800px;
}
.mui-card-footer{
    display: block;
}
.mui-card-footer button{
    margin: 10px 0px;
}
/* 小球 */
.ball{
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 99;
  left: 139px;
  top: 341px;
  /* transform: translate(138px,339px) */
  
}
</style>